package com.xxg.fileshand.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.xxg.fileshand.adapter.InventoryCaseAdapter
import com.xxg.fileshand.adapter.InventoryTaskAdapter
import com.xxg.fileshand.app.SetInfo
import com.xxg.fileshand.base.BaseVMActivity
import com.xxg.fileshand.base.NoViewModel
import com.xxg.fileshand.base.ext.getApiService
import com.xxg.fileshand.base.launch
import com.xxg.fileshand.bean.InventoryTaskResponse.DataDTO
import com.xxg.fileshand.constant.Constant
import com.xxg.fileshand.databinding.ActivityInventoryTaskBinding
import com.xxg.fileshand.greendao.GreenDaoManager

class InventoryTaskActivity : BaseVMActivity<ActivityInventoryTaskBinding, NoViewModel>() {

    private var inventoryTaskAdapter: InventoryTaskAdapter = InventoryTaskAdapter()
    private var caseAdapter: InventoryCaseAdapter = InventoryCaseAdapter()

    private var currentTaskId: String = ""
    private var currentPos = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mBinding.rvTask.adapter = inventoryTaskAdapter
        mBinding.rvCase.adapter = caseAdapter

        inventoryTaskAdapter.setOnItemClickListener { adapter, view, position ->
            inventoryTaskAdapter.data[position].pdCaseNo?.let {
                mBinding.rvCase.visibility = View.VISIBLE
                currentPos = position
                caseAdapter.setNewInstance(it)
                currentTaskId = inventoryTaskAdapter.data[position].pdTaskID
            }
        }

        caseAdapter.setOnItemClickListener { adapter, view, position ->
            caseAdapter.data[position].let {
                val intent = Intent(this, GoodsInventoryActivity::class.java)
                intent.putExtra(Constant.CASE_CODE, it.szWZCaseNo)
                intent.putExtra(Constant.CASE_NAME, it.szWZCaseNoTran)
                intent.putExtra(Constant.PD_TASK_ID, currentTaskId)
                startActivityForResult(intent, object : OnActivityCallback {
                    override fun onActivityResult(resultCode: Int, data: Intent?) {
                        if (resultCode == 1) {
                            data?.getStringExtra(Constant.CASE_CODE)?.let {
                                //删除已盘点层位
                                caseAdapter.data.removeAt(position)
                                caseAdapter.notifyItemRemoved(position)
                                if (caseAdapter.data.size == 0) {
                                    inventoryTaskAdapter.data.removeAt(currentPos)
                                    inventoryTaskAdapter.notifyItemRemoved(currentPos)
                                    mBinding.rvCase.visibility = View.GONE
                                }
                            }
                        }
                    }

                })
            }
        }

    }

    override fun onLeftClick() {
        super.onLeftClick()
        onBackPressed()
    }

    override fun getMyTitle(): String {
        return "盘点任务"
    }


}